<?php

/**
 * This is the model class for table "{{pages}}".
 *
 * The followings are the available columns in table '{{pages}}':
 * @property string $pageId
 * @property string $pageParent
 * @property string $pageType
 * @property string $pageUrl
 * @property string $pageTitle
 * @property string $pageContentShort
 * @property string $pageContentFull
 * @property string $pageImages
 * @property integer $pageActive
 * @property string $pageAuthor
 * @property integer $pageSort
 * @property string $pageAction
 * @property integer $pageItemId
 */
class Models extends CActiveRecord {

	public $pageCategory;

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{pages}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('pageUrl, pageTitle, pageActive', 'required'),
			array('pageParent', 'default'),
			array('pageImages', 'file', 'allowEmpty' => false, 'types' => 'jpg,png,bmp,jpeg', 'on' => 'create'),
			array('pageImages', 'file', 'allowEmpty' => true, 'types' => 'jpg,png,bmp,jpeg', 'on' => 'update'),
//			array('pageActive, pageSort, pageItemId', 'numerical', 'integerOnly'=>true),
//			array('pageParent, pageAuthor', 'length', 'max'=>11),
//			array('pageType', 'length', 'max'=>8),
//			array('pageUrl, pageTitle, pageImages, pageAction', 'length', 'max'=>255),
//			array('pageContentShort, pageContentFull', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('pageId, pageParent, pageType, pageUrl, pageTitle, pageContentShort, pageContentFull, pageImages, pageActive, pageAuthor, pageSort, pageAction, pageItemId', 'safe', 'on' => 'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'pageId' => 'ID',
			'pageParent' => 'Родительская бренд',
			'pageType' => 'Page Type',
			'pageUrl' => 'Адрес бренда',
			'pageTitle' => 'Название',
			'pageContentShort' => 'Page Content Short',
			'pageContentFull' => 'Page Content Full',
			'pageImages' => 'Изображение',
			'pageActive' => 'Активность',
			'pageAuthor' => 'Page Author',
			'pageCategory' => 'Категория',
			'pageAction' => 'Page Action',
			'pageItemId' => 'Page Item',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search() {
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('"pageId"', $this->pageId);
		$criteria->compare('"pageParent"', $this->pageParent, true);
		$criteria->compare('"pageType"', $this->pageType, true);
		$criteria->compare('"pageUrl"', $this->pageUrl, true);
		$criteria->compare('"pageTitle"', $this->pageTitle, true);
		$criteria->compare('"pageContentShort"', $this->pageContentShort, true);
		$criteria->compare('"pageContentFull"', $this->pageContentFull, true);
		$criteria->compare('"pageImages"', $this->pageImages, true);
		$criteria->compare('"pageActive"', $this->pageActive);
		$criteria->compare('"pageAuthor"', $this->pageAuthor, true);
		$criteria->compare('"pageSort"', $this->pageSort);
		$criteria->compare('"pageAction"', $this->pageAction, true);
		$criteria->compare('"pageItemId"', $this->pageItemId);

		$criteria->addCondition("(\"pageType\" = 'model')");
		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
			'pagination' => array(
				'pageSize' => 20,
				'pageVar' => 'page'
			),
		));
	}

	public function getModels() {
		$query = "SELECT \"pageId\", \"pageTitle\" FROM {{tree}} WHERE \"pageType\" = 'model'";
		$models = Yii::app()->db->createCommand($query)->queryAll(true);
		foreach ($models as $model) {
			$array[$model['pageId']] = $model['pageTitle'];
		}
		return $array;
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Brand the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

}
